//
//  MeViewController.m
//  Dear
//
//  Created by 陈俊俊 on 15/7/11.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "MeViewController.h"
#import "MeCell.h"
#import "MeCellFirst.h"

#import "MeDetailViewController.h"
#import "AdviceViewController.h"
#import "TPFileManager.h"
#import "AboutViewController.h"
@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
    UIImageView *_headImageView;
}
@end
@implementation MeViewController
{
    UITableView * _tableView;
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createTitleViewWith:@"我"];
    [self uiConfig];
    
}
- (void)uiConfig {
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, 0,ScreenSize.width , ScreenSize.height -64 - 49)) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = RGBA(246, 246, 246, 1);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}
#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }else {
        
        return 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        MeCellFirst * cell = [[[NSBundle mainBundle]loadNibNamed:@"MeCellFirst" owner:self options:nil]lastObject];
        
        NSString *filePhoto = @"";
        if (![TPFileManager getPhotoStr:[NSString stringWithFormat:@"%@%@",HeadImage,[DearUser sharedInstance].userId]]) {
            NSArray *images = [[DearUser sharedInstance].picture componentsSeparatedByString:@","];
            if (images.count == 0) {
                filePhoto = @"shoot";
            }else{
                filePhoto = images[0];
            }
        }else{
            filePhoto = HeadImage;
        }
        NSArray * arr = @[filePhoto,[DearUser sharedInstance].txtName,[DearUser sharedInstance].userName];
        [cell showDataWithArray:arr];
        _headImageView = cell.headImV;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
    }else {
        NSArray * declareArr =@[ @[@"",@"欢迎页",@"去分享",@"求关注"],@[@"检查更新",@"意见反馈",@"关于"]];
        NSArray * imageName =@[ @[@"",@"dear_first_item_bought",@"dear_first_item_webcinema",@"dear_first_item_exchange"],@[@"dear_first_item_offline",@"dear_first_item_histroy",@"dear_first_item_fovarite"]];
        static NSString * cellID = @"MeCell";
        MeCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"MeCell" owner:self options:nil]lastObject];
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell showDataWithImage:imageName[indexPath.section][indexPath.row] title:declareArr[indexPath.section][indexPath.row] ];
        return cell;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section== 0) {
        return 0;
    }else {
        return 44;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        
        UILabel * label = [[UILabel alloc]initWithFrame:(CGRectMake(10, 0, 100, 44))];
        label.text = @"   设置";
        label.textAlignment= NSTextAlignmentLeft;
        label.textColor = [UIColor grayColor];
        return label;
    }else {
        return nil;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 60;
    }else {
        return 42;
    }
    
}
#pragma mark -点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MeDetailViewController *uvc = [[MeDetailViewController alloc]init];
            uvc.hidesBottomBarWhenPushed = YES;
            uvc.getImageView = ^(void){
                 NSData *data = [TPFileManager getPhoto:[NSString stringWithFormat:@"%@%@",HeadImage,[DearUser sharedInstance].userId]];
                 _headImageView.image = [UIImage imageWithData:data];
            };
            [self.navigationController pushViewController:uvc animated:YES];
        }
    }else{
        if (indexPath.row == 0) {
            //检查更新
            [self checkVersion];
        }else if(indexPath.row == 1){
            AdviceViewController *avc = [[AdviceViewController alloc]init];
            avc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:avc animated:YES];
            
        }else{
            AboutViewController *avc = [AboutViewController new];
            avc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:avc animated:YES];
        }
    }
}

- (void)checkVersion{
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"versioncode":kVersionKey};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpCheckVersion];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"发现最新版本是否下载" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alert.delegate = self;
                [alert show];

            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        
    }else{
       NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id995211420"];
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_tableView) {
        [_tableView reloadData];
    }
}



@end

